import { createApp } from 'vue'
import './style.css'
import 'vue-select/dist/vue-select.css';
import App from './App.vue'
import axios from 'axios'
import { isLoggedIn, logout } from './api/authAPI.js'
import vSelect from 'vue-select'

// 设置全局axios默认配置
axios.defaults.timeout = 30000; // 30秒超时
axios.defaults.baseURL = ''; // 可以设置基础URL

// 添加请求拦截器，自动添加Authorization头
axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem('Authorization');
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 添加响应拦截器，处理401错误
axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    if (error.response?.status === 401) {
      // 清除登录状态
      logout();
      // 跳转到登录页面
      window.location.hash = '#/login';
    }
    return Promise.reject(error);
  }
);

const app = createApp(App)
app.component('v-select', vSelect)
app.mount('#app')
